package com.example.smartretaildemo.module.shoppingcart.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

import com.example.smartretaildemo.MainActivity;
import com.example.smartretaildemo.bean.shoppingcart.ShopCartInfo;
import com.example.smartretaildemo.databinding.ActivityQRcodeBinding;
import com.example.smartretaildemo.network.QRcode;
import com.example.smartretaildemo.network.sqliteopenhelper.MyDataAdapter;

import java.util.ArrayList;
import java.util.List;

public class QRcodeActivity extends AppCompatActivity {
    ActivityQRcodeBinding binding;
    private List<ShopCartInfo> payList;
    private String priceCount;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        binding = ActivityQRcodeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
//        取得数据
        payList = (ArrayList<ShopCartInfo>)getIntent().getSerializableExtra("payList");
        priceCount = getIntent().getStringExtra("priceCount");
//        二维码文字显示
        String str = "总价:￥" + priceCount + "\n";
        int id = 0;
        for (ShopCartInfo i : payList) {
            id++;
            String commodityId = i.getCommodityId();
            String commodityName = i.getCommodityName();
            String fCostPrice = String.valueOf(i.getfCostPrice());
            String num = String.valueOf(i.getNum());
            String pic = i.getPic();
            str += "\t" + id + "、 " + "ID:" + commodityId + ", Name:" + commodityName + ", Price:" + fCostPrice + ", Num:" + num + ", Pic:" + pic + "\n";
        }
//        使用Zxing实现二维码的生成
        QRcode qRcode = new QRcode();
        Bitmap bitmap = qRcode.qrcode(str);
        binding.imgShopQAPic.setImageBitmap(bitmap);
//        长按事件监听
        binding.imgShopQAPic.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Toast.makeText(QRcodeActivity.this, "购买成功", Toast.LENGTH_SHORT).show();
//                遍历结算列表实现售出
                for (ShopCartInfo i : payList) {
                    new MyDataAdapter().delete(QRcodeActivity.this, i.getCommodityId());
                }
                Intent intent = new Intent(QRcodeActivity.this, MainActivity.class);
//                返回将Tab显示为购物车界面
                intent.putExtra("CurrentTab", 2);
                startActivity(intent);
                finish();
                return true;
            }
        });
    }
}